package cn.lixu.org.controller;

import cn.lixu.basic.annotation.PreAuthorize;
import cn.lixu.basic.util.JsonResult;
import cn.lixu.basic.util.PageList;
import cn.lixu.org.domain.Department;
import cn.lixu.org.query.DepartmentQuery;
import cn.lixu.org.service.IDepartmentService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController //controller层  返回的是json格式数据
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;


    /**
     * 查询全部
     *
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询全部")
    @PreAuthorize(name = "查询全部部门列表",sn = "department:list") //value后面用来做权限校验
    public List<Department> loadAll() {
        return departmentService.loadAll();
    }

    /**
     * 查询一个
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询一个")
    @PreAuthorize(name = "查询一个部门列表",sn = "department:loadById") //value后面用来做权限校验
    public Department loadById(@PathVariable("id") Long id) {
        return departmentService.loadById(id);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @PreAuthorize(name = "删除",sn = "department:delete") //value后面用来做权限校验
    public JsonResult deleteById(@PathVariable("id") Long id) {
        try {
            departmentService.remove(id);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("删除失败");
        }
    }

    /**
     * @param department
     * @return
     */
    @PutMapping
    public JsonResult addAndUpdate(@RequestBody Department department) {
        try {
            if (department.getId() == null) {
                departmentService.save(department);
            } else {
                departmentService.update(department);
            }
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**
     * 分页数据
     *
     * @param departmentQuery
     * @return
     */
    @PostMapping
    public PageList<Department> queryPage(@RequestBody DepartmentQuery departmentQuery) {
        return departmentService.queryPage(departmentQuery);

    }

    /**
     * 批量删除
     *
     * @param
     * @return
     */
    @PatchMapping
    @PreAuthorize(name = "批量删除",sn = "department:delete") //value后面用来做权限校验
    public JsonResult patchDel(@RequestBody List<Long> ids) {
        try {
            departmentService.patchDel(ids);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("删除失败");
        }
    }

    /**
     * 查询部门树
     *
     * @return
     */
    @GetMapping("/deptTree")
    @ApiOperation(value = "获取部门树-无限极接口")
    public List<Department> deptTree() {
        return departmentService.deptTree();
    }
}

